$().ready(function() {
    validateRule();
    var prefix = ctx +"module/device/welding/record"
    layui.use(['form','table','element'], function(){
        var table = layui.table, form = layui.form ;
        var searchParams = {};
        searchParams['search_eq_isSelf']=$('#isSelf').val();
        searchParams['search_eq_deviceId']=$('#deviceId').val();
        //初始化table
        table.render({
            elem: '#exampleTable',//承载数据的table Id
            url: prefix + "/list",//请求数据连接
            // toolbar: '#toolbarDemo',
            cellMinWidth: 80,//全局定义所有常规单元格的最小宽度（默认：60）
            even: true,//是否开启隔行变色
            loading:true,//是否显示加载条
            // height: 'full-75',
            where :{
                searchParams:searchParams,
                sorts:{"id":"desc"}
            },
            //page: true,//是否分页
            page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: [ 'prev', 'page', 'next'] //自定义分页布局
            },
            cols: [[
                { field : 'useState',  title : '记录类型', align : 'center',
                    templet: function (item) {
                        if(item.useState == 0){
                            return '设备归还';
                        } else{
                            return '使用申请';
                        }
                    }
                },
                { field : 'createTime',  title : '申请时间', align : 'center', },
            ]],
            id: 'currentTableId',//表格Id
        });


        form.on('select(isHasBreaker)', function (data) {
            //alert(data.value);
            console.log('isHasBreaker',data.value);
            selectHasBreaker(data.value,form)
        });


        form.on('select(provId)', function (data) {
            //alert(data.value);
            console.log('provId',data.value);
            getCityList(data.value,form)
        });

    });
});


function selectHasBreaker(val,form){
    console.log('selectHasBreaker', val)

    $("#divFsrBreakerId").addClass("layui-hide");
    if(val == "1"){
        $("#divFsrBreakerId").removeClass("layui-hide");
    }
}

function getCityList(provId,form) {
    var url = ctx +"module/device/cityList/" + provId;
    $.ajax({
        cache : true,
        type : "GET",
        url : url,
        // data : $('#signupForm').serialize(),// 你的formid
        async : false,
        error : function(request) {
            parent.layer.alert("Connection error");
        },
        success : function(data) {
            if (data.code == 0) {

                $("#cityId").empty()
                $('#cityId').append(new Option('所在城市', ''));
                console.log('getCityList',data.cityList)
                for (var index in data.cityList){
                    $('#cityId').append(new Option(data.cityList[index].cityName, data.cityList[index].id));
                }
                form.render("select");

            } else {
                parent.layer.alert(data.msg)
            }
        }
    });
}




function update() {
	var url = ctx +"module/device/welding/" + ($('#signupForm').find("#id").val() == '' ? "save" : "update");
    $.ajax({
        cache : true,
        type : "POST",
        url : url,
        data : $('#signupForm').serialize(),// 你的formid
        async : false,
        error : function(request) {
            parent.layer.alert("Connection error");
        },
        success : function(data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoadSave();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
            }
        }
    });
}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules : {
            sort : {
                required : true
            }
        },
        messages : {
            sort : {
                required : icon + "请输入索引"
            }
        }
    })
}
